package com.xinlan.imageeditlibrary.editimage.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.xinlan.imageeditlibrary.R;

import java.util.ArrayList;

/**
 * 内置美颜的适配器
 */
public class MeiYanAdapter extends RecyclerView.Adapter<MeiYanAdapter.FilterItemViewHolder> {
    private Context mContext;
    private ArrayList<Integer> mFilters = new ArrayList<>();
    private ArrayList<String> mFiltersName = new ArrayList<>();
    private ClickCallBack mClickCallBack;
    private LinearLayout mLastLlFilterShape;
    private int mIndexPoation = -1;

    public interface ClickCallBack {
        void doSomeThing(int index);
    }

    public void setCallBack(ClickCallBack callBack) {
        this.mClickCallBack = callBack;
    }

    public MeiYanAdapter(Context context) {

        mContext = context;

        mFilters.clear();
        mFilters.add(R.drawable.qn_none_filter);
        mFilters.add(R.drawable.icon_meiyan);
        mFilters.add(R.drawable.icon_meibai);
        mFilters.add(R.drawable.icon_hongrun);
        mFilters.add(R.drawable.icon_hongrun);

        mFiltersName.clear();
        mFiltersName.add("无");
        mFiltersName.add("美颜");
        mFiltersName.add("美白");
        mFiltersName.add("红润");
        mFiltersName.add("");
    }


    public void setInedxPosation(int posation) {
        this.mIndexPoation = posation;
    }

    @NonNull
    @Override
    public FilterItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View contactView = inflater.inflate(R.layout.item_filter_meiyan, parent, false);
        return new FilterItemViewHolder(contactView);
    }

    @Override
    public void onBindViewHolder(@NonNull final FilterItemViewHolder holder, int position) {
        if (!mFiltersName.get(position).equals("")) {
            if (position == 0) {
                holder.mLine.setVisibility(View.VISIBLE);
            } else {
                holder.mLine.setVisibility(View.GONE);
            }
            holder.mIcon.setBackgroundResource(mFilters.get(position));
            holder.mName.setText(mFiltersName.get(position));
        } else {
            holder.mIcon.setVisibility(View.GONE);
            holder.mName.setVisibility(View.GONE);
        }

        if (mIndexPoation != -1 && mIndexPoation == position) {
            Drawable drawable = mContext.getResources().getDrawable(R.drawable.shape_meiyan_select);
            holder.mLlMeiYanShape.setBackground(drawable);
            mLastLlFilterShape = holder.mLlMeiYanShape;
        } else if (mIndexPoation == -1 && 0 == position) {
            Drawable drawable = mContext.getResources().getDrawable(R.drawable.shape_meiyan_select);
            holder.mLlMeiYanShape.setBackground(drawable);
            mLastLlFilterShape = holder.mLlMeiYanShape;
        }
        holder.mItemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mLastLlFilterShape != null) {
                    mLastLlFilterShape.setBackground(null);
                }
                Drawable drawable = mContext.getResources().getDrawable(R.drawable.shape_meiyan_select);
                holder.mLlMeiYanShape.setBackground(drawable);
                mLastLlFilterShape = holder.mLlMeiYanShape;
                mClickCallBack.doSomeThing(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mFilters != null ? mFilters.size() : 0;
    }


    class FilterItemViewHolder extends RecyclerView.ViewHolder {
        ImageView mIcon;
        TextView mName;
        LinearLayout mItemLayout;
        View mLine;
        LinearLayout mLlMeiYanShape;

        public FilterItemViewHolder(View itemView) {
            super(itemView);
            mIcon = itemView.findViewById(R.id.filter_image);
            mName = itemView.findViewById(R.id.filter_name);
            mLine = itemView.findViewById(R.id.filter_line);
            mLlMeiYanShape = itemView.findViewById(R.id.ll_meiyan_shape);
            mItemLayout = itemView.findViewById(R.id.ll_item_layout);
        }
    }
}
